using System;
using System.ComponentModel;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Shell;

namespace Red.Scripts.Options
{
    [Guid( Defines.Guids.Options.General.Raw )]
    public class PageGeneral : DialogPage
    {
        private Defines.Settings _settings;

        #region C-Tor

        public PageGeneral()
        {
            var componentModel = (IComponentModel)ServiceProvider.GlobalProvider.GetService( typeof( SComponentModel ) );
            _settings = componentModel.GetService< Defines.Settings >();

            InitializeDefaultValues();
        }

        #endregion C-Tor

        #region Private Methods

        void InitializeDefaultValues()
        {
            Type page = typeof( PageGeneral );
            PropertyInfo[] props = page.GetProperties( BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public );

            foreach ( PropertyInfo prop in props )
            {
                DefaultValueAttribute attr = (DefaultValueAttribute)Attribute.GetCustomAttribute( prop, typeof( DefaultValueAttribute ) );
                prop.SetValue( this, attr.Value );
            }

        }

        #endregion Private Methods

        #region Properties

        [Category( "Intellisense" )]
        [DisplayName( "Intellisense tab behaviour" )]
        [Description( "If set, pressing tab will switch between the available categories in the autocomplete popup. Otherwise, tab will insert the selected item" )]
        [DefaultValue( Defines.IntellisenseTabBehaviour.SwitchCategory )]
        public Defines.IntellisenseTabBehaviour IntellisenseTabBehaviour
        {
            get { return (Defines.IntellisenseTabBehaviour)_settings[ Defines.Setting.IntellisenseTabBehaviour ]; }
            set { _settings[ Defines.Setting.IntellisenseTabBehaviour ] = value; }
        }

        [Category( "Intellisense" )]
        [DisplayName( "Autocomplete on space" )]
        [Description( "Spacebar will insert the selected item into the selected item" )]
        [DefaultValue( false )]
        public bool AutocompleteOnSpacebar
        {
            get { return (bool)_settings[ Defines.Setting.AutocompleteOnSpace ]; }
            set { _settings[ Defines.Setting.AutocompleteOnSpace ] = value; }
        }

        [Category( "Intellisense" )]
        [DisplayName( "Highlight Matching Word" )]
        [Description( "Global: If a word is selected, highlight every instance of that word. Disabled: Turn the feature off" )]
        [DefaultValue( Defines.HighlightMatchingWordBehaviour.Global )]
        public Defines.HighlightMatchingWordBehaviour HighlightMatchingWordBehaviour
        {
            get { return (Defines.HighlightMatchingWordBehaviour)_settings[ Defines.Setting.HighlightMatchingWordBehaviour ]; }
            set { _settings[ Defines.Setting.HighlightMatchingWordBehaviour ] = value; }
        }

        #endregion Properties
    }
}
